package com.example.application.views.main;

import java.util.Optional;

import com.example.application.bean.User;
import com.example.application.views.petStore.PetStoreView;
import com.example.application.views.petSupplier.PetSupplierView;
import com.example.application.views.regiest.EnrollView;
import com.example.application.views.regiest.RegiestView;
import com.example.application.views.store.StoGoodsView;
import com.example.application.views.store.StoServiceView;
import com.example.application.views.store.SupGoodsView;
import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.ComponentUtil;
import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.applayout.AppLayout;
import com.vaadin.flow.component.applayout.DrawerToggle;
import com.vaadin.flow.component.avatar.Avatar;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.Image;
import com.vaadin.flow.component.html.H1;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.tabs.Tab;
import com.vaadin.flow.component.tabs.Tabs;
import com.vaadin.flow.component.tabs.TabsVariant;
import com.vaadin.flow.router.RouterLink;
import com.vaadin.flow.server.PWA;
import com.vaadin.flow.server.VaadinSession;
import com.vaadin.flow.theme.Theme;
import com.vaadin.flow.router.PageTitle;
import com.example.application.views.admin.AdminView;
import com.example.application.views.pet.PetView;
import com.example.application.views.supplier.SupplierView;
import com.example.application.views.store.StoreView;
import com.example.application.views.map.MapView;
import com.example.application.views.count.CountView;
import com.vaadin.flow.component.dependency.JsModule;
import com.vaadin.flow.theme.lumo.Lumo;
import com.vaadin.flow.component.dependency.CssImport;

/**
 * The main view is a top-level placeholder for other views.
 */
@PWA(name = "pet", shortName = "pet", enableInstallPrompt = false)
@JsModule("./styles/shared-styles.js")
@Theme(value = Lumo.class, variant = Lumo.DARK)
@CssImport("./views/main/main-view.css")
public class MainView extends AppLayout {

    private Tabs menu;
    private H1 viewTitle;
    private Dialog dialog=new Dialog();

    private User user = null;

    public MainView() {

        VaadinSession vs = UI.getCurrent().getSession();
        this.user = (User) vs.getAttribute("user");
        if(user == null){
            dialog.setCloseOnEsc(false);
            dialog.setCloseOnOutsideClick(false);
            dialog.removeAll();
            dialog.add(new Text("请先登录"));
            dialog.add(new Div(new Button("去登陆",e->{
                dialog.close();
                UI.getCurrent().navigate("login");
            })));
            dialog.open();
            this.menu = new Tabs();
            viewTitle = new H1();
            menu.add(createMenuItems2());
        }else{
            setPrimarySection(Section.DRAWER);
            addToNavbar(true, createHeaderContent());
            menu = createMenu();
            addToDrawer(createDrawerContent(menu));
        }

    }

    private Component createHeaderContent() {
        HorizontalLayout layout = new HorizontalLayout();
        layout.setId("header");
        layout.getThemeList().set("dark", true);
        layout.setWidthFull();
        layout.setSpacing(false);
        layout.setAlignItems(FlexComponent.Alignment.CENTER);
        layout.add(new DrawerToggle());
        viewTitle = new H1();
        layout.add(viewTitle);
        layout.add(new Avatar());
        return layout;
    }

    private Component createDrawerContent(Tabs menu) {
        VerticalLayout layout = new VerticalLayout();
        layout.setSizeFull();
        layout.setPadding(false);
        layout.setSpacing(false);
        layout.getThemeList().set("spacing-s", true);
        layout.setAlignItems(FlexComponent.Alignment.STRETCH);
        HorizontalLayout logoLayout = new HorizontalLayout();
        logoLayout.setId("logo");
        logoLayout.setAlignItems(FlexComponent.Alignment.CENTER);
        logoLayout.add(new Image("images/logo.png", "pet logo"));
        logoLayout.add(new H1("pet"));
        layout.add(logoLayout, menu);
        return layout;
    }

    private Tabs createMenu() {
        final Tabs tabs = new Tabs();
        tabs.setOrientation(Tabs.Orientation.VERTICAL);
        tabs.addThemeVariants(TabsVariant.LUMO_MINIMAL);
        tabs.setId("tabs");

        if("门店管理员".equals(this.user.getIdentity())){
//            门店管理员页面
            tabs.add(createStoreMenuItems());
        }else if("供应商管理员".equals(this.user.getIdentity())){
//            供应商管理员页面
            tabs.add(createSupplierMenuItems());
        }else if("平台管理员".equals(this.user.getIdentity())){
//            用户管理员页面
            tabs.add(createAdminMenuItems());
        }else{
            tabs.add(createMenuItems2());
        }
        return tabs;
    }

    private Component[] createMenuItems() {
        return new Tab[]{
//                createTab("测试分页", PageTestView.class),
//                createTab("注册", EnrollView.class)
        };
    }

    private Component[] createAdminMenuItems() {
        return new Tab[]{
                createTab("用户管理", AdminView.class),
                createTab("申请列表", RegiestView.class),
                createTab("宠主管理", PetView.class),
                createTab("门店管理", StoreView.class),
                createTab("供应商管理", SupplierView.class)
        };
    }

    private Component[] createStoreMenuItems() {
        return new Tab[]{
                createTab("门店管理", StoreView.class),
                createTab("商品管理", StoGoodsView.class),
                createTab("服务管理", StoServiceView.class),
                createTab("进购货物", SupGoodsView.class),
                createTab("统计", CountView.class),
                createTab("地图", MapView.class)
        };
    }

    private Component[] createSupplierMenuItems() {
        return new Tab[]{
                createTab("供应商管理", SupplierView.class),
                createTab("供应货品管理", PetSupplierView.class),
                createTab("统计", CountView.class)
        };
    }

    private Component[] createMenuItems2() {
        return new Tab[]{
        };
    }

    private static Tab createTab(String text, Class<? extends Component> navigationTarget) {
        final Tab tab = new Tab();
        tab.add(new RouterLink(text, navigationTarget));
        ComponentUtil.setData(tab, Class.class, navigationTarget);
        return tab;
    }

    @Override
    protected void afterNavigation() {
        super.afterNavigation();
        getTabForComponent(getContent()).ifPresent(menu::setSelectedTab);
        viewTitle.setText(getCurrentPageTitle());
    }

    private Optional<Tab> getTabForComponent(Component component) {
        return menu.getChildren().filter(tab -> ComponentUtil.getData(tab, Class.class).equals(component.getClass()))
                .findFirst().map(Tab.class::cast);
    }

    private String getCurrentPageTitle() {
        PageTitle title = getContent().getClass().getAnnotation(PageTitle.class);
        return title == null ? "" : title.value();
    }
}
